import time

from page.base_page import BasePage
from appium.webdrive.common.appiumby import AppiumBy

from page.personal_detail_page import PersonalDetailPage


class SearchMemberPage(BasePage):
    def __init__(self, driver):
        super().__init__(driver)

    # 返回姓名（昵称）列表，只返回当前可见的部分（暂时够用）
    def search(self, key):
        self.find(AppiumBy.CSS_SELECTOR, 'android.widget.EditText').clear().send_keys(key)
        time.sleep(1)
        name_elements = self.finds(AppiumBy.XPATH,
                                   '//android.widget.ListView//android.view.ViewGroup/android.widget.TextView')
        return [name_element.text for name_element in name_elements]

    # 选择列表的第n行，进入详情页
    def to_search_result_detail(self, n = 0) -> PersonalDetailPage:
        g9n_list = self.finds(AppiumBy.XPATH, '//android.widget.ListView//android.view.ViewGroup')
        g9n_list[n].click()
        return PersonalDetailPage(self.driver)